package com.springboot.mybatis.service.impl;

import com.springboot.mybatis.entity.Order;
import com.springboot.mybatis.entity.User;
import com.springboot.mybatis.repository.OrderRepository;
import com.springboot.mybatis.repository.UserRepository;
import com.springboot.mybatis.service.UserService;
import com.springboot.mybatis.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @version V1.0
 * @program: mybatis
 * @description
 * @author: 张要廷<zhang_yt @ suixingpay.com>
 * @create: 2019/10/21 19:00
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private OrderRepository orderRepository;

    @Override
    public int saveUserAllParam(User user) {
        if (user == null){
            log.error("inster user,user is null");
            return 0;
        }
        String id = UUIDUtil.getUUID32();
        user.setId(id);
        userRepository.insert(user);
        //制造订单数据信息
        for (int i = 2;i < 4;i++){
            this.saveOrders(id,i);
        }
        return 1;
    }

    @Override
    public int saveUserSelective(User user) {
        if (user == null){
            log.error("inster user,user is null");
            return 0;
        }
        String id = UUIDUtil.getUUID32();
        user.setId(id);
        userRepository.insertSelective(user);
        for (int i = 0;i < 2;i++){
            this.saveOrders(id,i);
        }
        return 1;
    }

    /**
     * 制造订单信息
     * @param userId
     */
    private void saveOrders(String userId,int num){
        SimpleDateFormat formart = new SimpleDateFormat("yyyyMMddhhmmss");
        String orderNo = formart.format(new Date());
        int ordernum = (int) ((Math.random() * 9 + 1) * 100000);
        Order order = Order.builder().id(UUIDUtil.getUUID32())
                .orderNo(orderNo+ordernum)
                .productName("产品名称"+num)
                .productPrice(BigDecimal.valueOf(Double.parseDouble("12.45")))
                .productNum(4)
                .userId(userId).build();
        orderRepository.insertSelective(order);
    }

    @Override
    public List<User> findByUserName(String userName) {
        if (StringUtils.isEmpty(userName)){
            log.error("findByUserName,userName=null");
            return null;
        }
        List<User> users = userRepository.selectByUserName(userName);
        return users;
    }

    @Override
    public List<User> findByUserName2(String userName) {
        if (StringUtils.isEmpty(userName)){
            log.error("findByUserName2,userName=null");
        }
        List<User> users = userRepository.selectByUserName2(userName);
        return users;
    }

    @Override
    public User findOne(User user) {

        return userRepository.selectOne(user);
    }

    @Override
    public User findOne1(User user) {
        return userRepository.selectOne1(user);
    }

    @Override
    public User findOne2(User user) {
        return userRepository.selectOne2(user);
    }

    @Override
    public List<User> findUsersByNames(List<String> userNameList) {
        if (CollectionUtils.isEmpty(userNameList)){
            log.error("findUsersByNames,userNameList=null");
            return null;
        }
        return userRepository.selectUsersByNames(userNameList);
    }

    @Override
    public User findUserOrdersById(String id) {
        if (StringUtils.isEmpty(id)){
            log.error("findUserOrdersById,id=null");
            return null;
        }
        return userRepository.selectUserOrdersById(id);
    }

    @Override
    public User findUserOrdersById2(String id) {
        if (StringUtils.isEmpty(id)){
            log.error("findUserOrdersById2,id=null");
            return null;
        }
        return userRepository.selectUserOrdersById2(id);
    }

    @Override
    public int delUserById(String id) {
        if (StringUtils.isEmpty(id)){
            log.error("delUserById,id=null");
            return 0;
        }
        return userRepository.deleteById(id);
    }

    @Override
    public int updateUserByPrimaryKeySelective(User user) {
        if (user == null || StringUtils.isEmpty(user.getId())){
            log.error("updateUserByPrimaryKeySelective,user=null");
            return 0;
        }
        return userRepository.updateByPrimaryKeySelective(user);
    }

    @Override
    public User findUserById(String id) {
        return userRepository.findUserById(id);
    }

    @Override
    public User findUserById2(String id) {
        return userRepository.findUserById2(id);
    }

    @Override
    public User findUserByIdProvider(String id) {
        return userRepository.getUserById(id);
    }
}
